using System;
using System.Collections.Generic;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class SkinMissionController : SingletonMB<SkinMissionController>
{
	public List<SkinMission> AllMissions = new List<SkinMission>();

	public static string UnlockedItem
	{
		get
		{
			return NintendoPlayerPrefs.GetString("Unlocked_Item");
		}
		set
		{
			NintendoPlayerPrefs.SetString("Unlocked_Item", value);
		}
	}

	private void Start()
	{
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(OnGameStart));
		long num = DateTime.Now.ToBinary();
		if (!Preloader.MinecraftLevelConfig.IsEnabled)
		{
			AllMissions.RemoveAt(AllMissions.Count - 1);
		}
		if (NintendoPlayerPrefs.HasKey("Skin_Mission_Date") && long.TryParse(NintendoPlayerPrefs.GetString("Skin_Mission_Date"), out var result))
		{
			DateTime dateTime = DateTime.FromBinary(result);
			if ((DateTime.Now - dateTime).TotalSeconds > 5.0)
			{
				ProgressMission(SkinMissionType.Days, 1);
			}
		}
		NintendoPlayerPrefs.SetString("Skin_Mission_Date", num.ToString() ?? "");
	}

	private void OnDestroy()
	{
		if (SingletonMB<GameManager>.Instance != null)
		{
			GameManager instance = SingletonMB<GameManager>.Instance;
			instance.OnGameStart = (Action)Delegate.Remove(instance.OnGameStart, new Action(OnGameStart));
		}
	}

	private void OnGameStart()
	{
		UnlockedItem = null;
	}

	public SkinMission GetMission(Item item)
	{
		foreach (SkinMission allMission in AllMissions)
		{
			if (allMission.LinkedSkin.ID == item.ID)
			{
				return allMission;
			}
		}
		return null;
	}

	public void ProgressMission(SkinMissionType missionType, int amount)
	{
		if (!Preloader.ShopConfig.ShopMissions || SingletonMB<OnboardingManager>.Instance.IsOnboardingActive || Preloader.SkinsByRV.IsEnabled)
		{
			return;
		}
		foreach (SkinMission allMission in AllMissions)
		{
			if (allMission.MissionType != missionType)
			{
				continue;
			}
			allMission.Progress += amount;
			if (allMission.Progress >= allMission.Goal && allMission.LinkedSkin != null && InventoryManager.Instance.HasInventory(allMission.LinkedSkin))
			{
				InventoryManager.Instance.UnlockItem(allMission.LinkedSkin);
				UnlockedItem = allMission.LinkedSkin.ID;
				if (missionType == SkinMissionType.UpgradeLength)
				{
					SingletonMB<GameManager>.Instance.TryShowSkinUnlock();
				}
			}
		}
	}

	internal string GetText(Item item)
	{
		foreach (SkinMission allMission in AllMissions)
		{
			if (allMission.LinkedSkin != null && allMission.LinkedSkin.ID == item.ID)
			{
				return allMission.GetStatus();
			}
		}
		return "";
	}
}
